//
//  SLBaseWKWebView.m
//  ShopLeader
//
//  Created by 帅科 on 2020/5/25.
//  Copyright © 2020 ShopLeader. All rights reserved.
//

#import "QSBaseWebView.h"
#import "SLWKProcessPool.h"
#import "SLWeakScriptMessageHandler.h"

#define OBSERVE_READY_STATE_MESSAGE_HANDLER_NAME    @"ObserveReadyState"
#define kSLDocumentStateHandler                     @"documentStateHandler"

@interface QSBaseWebView ()

@end

@implementation QSBaseWebView

-(instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration
{
    [self injectWithConfiguration:configuration];
    
    if (self = [super initWithFrame:frame configuration:configuration]) {
        [self sl_initWKProcessPoolWithConfiguration:configuration];
    }
    return self;
}

/// 注入js
- (void)injectWithConfiguration:(WKWebViewConfiguration *)configuration{
        
    
    WKUserScript *script1 = [[WKUserScript alloc] initWithSource:[NSString stringWithFormat:@"window.document.onreadystatechange= (state)=>{window.webkit.messageHandlers.%@.postMessage(document.readyState)};",kSLDocumentStateHandler]
                                                  injectionTime:WKUserScriptInjectionTimeAtDocumentStart
                                               forMainFrameOnly:NO];
    [configuration.userContentController addUserScript:script1];
    
    SLWeakScriptMessageHandler *scriptHandler = [[SLWeakScriptMessageHandler alloc]initWithDelegate:self];

    [configuration.userContentController addScriptMessageHandler:scriptHandler name:kSLDocumentStateHandler];
    
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    NSURL *url = [bundle URLForResource:@"QSWKWebview" withExtension:@"bundle"];
    NSBundle *qsBundle = [NSBundle bundleWithURL:url];
    NSString* infoImage = [qsBundle pathForResource:@"dsBridge" ofType:@"js"];
    
    NSString *scource2 = [NSString stringWithContentsOfFile:infoImage encoding:NSUTF8StringEncoding error:nil];
    WKUserScript * uScript2 = [[WKUserScript alloc] initWithSource:scource2 injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
    [configuration.userContentController addUserScript:uScript2];
    
    
    WKUserScript *script2 = [[WKUserScript alloc] initWithSource:@"window.UniqueJSBridge=window.dsBridge;"
                                                  injectionTime:WKUserScriptInjectionTimeAtDocumentStart
                                               forMainFrameOnly:NO];
    [configuration.userContentController addUserScript:script2];
}

- (void)sl_initWKProcessPoolWithConfiguration:(WKWebViewConfiguration *)configuration
{
    configuration.allowsInlineMediaPlayback = YES;
    configuration.suppressesIncrementalRendering = NO;
    if (@available(iOS 9.0, *)) {
        configuration.allowsAirPlayForMediaPlayback =YES;
    } else {
        // Fallback on earlier versions
    }
    if (@available(iOS 10.0, *)) {
        configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
    } else {
        // Fallback on earlier versions
    }
    if (@available(iOS 10.0, *)) {
        configuration.mediaTypesRequiringUserActionForPlayback = false;
    } else {
        // Fallback on earlier versions
    }
}


- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
    if ([message.name isEqualToString:kSLDocumentStateHandler]) {
        SLDocumentReadyStateType type = [self currentDocumentWithState:message.body];
        !_webViewDocumentStateBlock?:_webViewDocumentStateBlock(type);
    }
    !_webViewScriptHandlerBlock?:_webViewScriptHandlerBlock(self,message);
}

- (SLDocumentReadyStateType)currentDocumentWithState:(NSString*)stateStr{
    
    if ([stateStr isEqualToString:@"interactive"]) {
        return SLDocumentReadyState_interactive;
    }else if ([stateStr isEqualToString:@"complete"]){
        return SLDocumentReadyState_complete;
    }else{
        return SLDocumentReadyState_loading;
    }
}

- (void)dealloc
{
    NSLog(@"%s",__func__);
}


@end
